package com.huwei.modules.base.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.beans.factory.annotation.Autowired;

import com.huwei.modules.base.constant.JsonResultConstant;
import com.huwei.modules.base.constant.SystemConstant;
import com.huwei.modules.base.properties.JsonResultProperties;
import com.huwei.modules.base.properties.JwtProperties;
import com.huwei.modules.base.properties.SystemProperties;
import com.huwei.modules.base.util.jwt.JwtUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebListener
public class ArgumentInitializeListener implements ServletContextListener {

	@Autowired
	private JsonResultProperties jsonResultProperties;
	@Autowired 
	private SystemProperties systemProperties;
	@Autowired
	private JwtProperties jwtProperties;

	public void contextInitialized(ServletContextEvent sce) {
		log.info("---> application starting and to initialize argument !!!");
		initializeApplicationArgument();
	}

	public void contextDestroyed(ServletContextEvent sce) {
	}

	private void initializeApplicationArgument() {
		// 初始化json编码约定
		JsonResultConstant.initialzeJsonResultConstant(jsonResultProperties.getConstant());

		// 初始化系统参数
		SystemConstant.initialzeJsonResultConstant(systemProperties.getSys());
		
		//初始化jwt工具
		JwtUtil.buildDefined(jwtProperties.getExpTime(), jwtProperties.getExpGraTime(), jwtProperties.getIssuser()
				, jwtProperties.getAudience(), jwtProperties.getHeadType(), jwtProperties.getHeadAlg());
	}
}
